package com.lutzyt.delivery.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lutzyt.delivery.bean.Address;
import com.lutzyt.delivery.service.AddressService;
import com.lutzyt.delivery.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * controller 控制器
 *
 * @author zyt
 * @date 2021-05-29
 */
@Slf4j
@RestController
@RequestMapping("/api/address")
public class AddressController {
  private AddressService service;

  @GetMapping("/{id}")
  public Result<Address> getById(@PathVariable Integer id) {
    final Address bean = service.findById(id);
    return Result.success(bean);
  }

  @PutMapping("/{id}")
  public Result<Void> update(@RequestBody Address bean) {
    log.info("更新菜单:{}", bean);
    service.update(bean);
    return Result.success();
  }

  @PutMapping("/update/{id}")
  public Result<Void> customerUpdate(@RequestBody Address bean) {
    log.info("更新菜单:{}", bean);
    service.customerUpdate(bean);
    return Result.success();
  }

  @PostMapping("/page")
  public Result<IPage<Address>> page(Integer pageSize, Integer pageNum) {
    final IPage<Address> page = service.findByPage(pageNum, pageSize);
    return Result.success(page);
  }

  @DeleteMapping("/{id}")
  public Result<Void> del(@PathVariable Integer id) {
    log.info("删除:{}", id);
    service.deleteById(id);
    return Result.success();
  }

  @DeleteMapping
  public Result<Void> del(@RequestParam("id") List<String> ids) {
    log.info("pl删除:{}", ids);
    service.deleteByIds(ids);
    return Result.success();
  }

  @PostMapping
  public Result<Void> insert(@RequestBody Address bean, @SessionAttribute("userId")Integer userId) {
    log.info("添加菜单:{}", bean);
    bean.setCustomerId(userId);
    service.insert(bean);
    return Result.success();
  }

  @PostMapping("/create")
  public Result<Void> create(@RequestBody Address bean) {
    log.info("添加菜单:{}", bean);
    service.create(bean);
    return Result.success();
  }

  @Autowired
  public void setService(AddressService service) {
    this.service = service;
  }
}
    